﻿using SwaggerJwtAuthenticationCenter.DbHelper.Base;
using SwaggerJwtAuthenticationCenter.Helper;
using SwaggerJwtAuthenticationCenter.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.IO;
using SwaggerJwtAuthenticationCenter.Models.SysModel;
using SwaggerJwtAuthenticationCenter.Models.UserModel;

namespace SwaggerJwtAuthenticationCenter.DbHelper
{
    public class LoginService: SimpelRepository, ILoginService,IBaseRepository
    {
        public LoginService(List<MultiDBConfig> dBConfigs) :base(dBConfigs)
        { 
            
        }
        #region 新增用户
        public ResponseMsg<string> AddUser(UserApplication user)
        {
            sys_user sys_User = new sys_user { };
            return null
        }
        #endregion


        #region DBFirst
        /// <summary>
        /// 创建模型
        /// </summary>
        /// <param name="dirPath"></param>
        /// <param name="nameSpace"></param>
        /// <param name="connID"></param>
        /// <returns></returns>
        public ResponseMsg<string> CreateModelFile(string dirPath, string nameSpace, string connID = null)
        {
            DirectoryInfo directoryInfo = new DirectoryInfo(dirPath);
            if (!directoryInfo.Exists) directoryInfo.Create();
            if (!string.IsNullOrWhiteSpace(connID)) this._sqlSugarClient.ChangeDatabase(connID);
            this._sqlSugarClient.DbFirst.CreateClassFile(dirPath, nameSpace);
            var files = directoryInfo.GetFiles("*", SearchOption.AllDirectories);
            var result = files != null && files.Length > 0;
            return new ResponseMsg<string> { result = result, message = result ? "创建Model成功" : "创建Model失败", data = $@"文件路径{dirPath}" };
        }
        #endregion

        #region 登录获取Token
        public ResponseMsg<LoginResultModel> Login(LoginModel loginModel)
        {
            try
            {
                var model = this.QuerySingle<sys_user>(e => e.UserCode == loginModel.userCode).Result;
                if (model is null) throw new Exception("未找到当前用户");
                if (model.UserName != loginModel.userName) throw new Exception("校验姓名错误");
                if (model.Password != loginModel.password) throw new Exception("密码输入有误");
                string role="", department = "";

                var roleMaps = this.Query<sys_userRoleMap>(e => e.UserCode == loginModel.userCode).Result;
                if(roleMaps!=null&&roleMaps.Count>0)
                {
                    role = string.Join(',', roleMaps.Select(r => r.RoleCode));
                }
                var userDepts = this.Query<sys_userOrganizeMap>(e => e.UserCode == loginModel.userCode).Result;
                if (userDepts != null && userDepts.Count > 0)
                {
                    department = string.Join(',', userDepts.Select(r => r.OrganizeCode));
                }
                LoginResultModel resultModel = new LoginResultModel
                {
                    userCode = loginModel.userCode,
                    userName = loginModel.userName,
                    role=role,
                    department= department

                };
                return new ResponseMsg<LoginResultModel> { result = true, message = "校验成功,用户登录成功",data= resultModel };
            }
            catch(Exception ex)
            {
                return new ResponseMsg<LoginResultModel> { result = false, message = $"获取Token失败，{ex.Message}",data=null };
            }
         }
        #endregion

    }
}
